/* 
 * File:   VectorROICreator.cpp
 * Author: Guilherme Monteiro
 */

#include "../headers/VectorROIsCreator.h"

/**
 * Constructor of the class.
 * 
 * @param highResolutionImage
 */
VectorROIsCreator::VectorROIsCreator(TranscriberMetaInfo* metaInfo, bool isRotated ) {
    this->metaInfo = metaInfo;
    this->isRotated = isRotated;
    setHighResolutionImage(this->metaInfo->getHighResolutionImagePath());
}

VectorROIsCreator::VectorROIsCreator(const VectorROIsCreator& orig) {
}

VectorROIsCreator::~VectorROIsCreator() {
}

/**
 * Configures the image about
 * it works.
 * 
 * @param highResolutionImagePath
 */
void VectorROIsCreator::setHighResolutionImage(string highResolutionImagePath) throw (NullImageException) {
    this->highResolutionImage = cvLoadImage(highResolutionImagePath.c_str());
    if(this->highResolutionImage == 0) {
        throw NullImageException(highResolutionImagePath);
    }
}

/**
 * Create the vector of rois based on
 * the image loaded from path passed
 * in constructor. Each page contains one
 * micro image (a roi) encapsuled into an object (ROI)
 * with the data
 * read of file in metadados/entrada/ folder.
 *
 * At final the method of cut is called here.
 */
void VectorROIsCreator::createROIs() {
    Reader* reader = new Reader(this->metaInfo->getInputFilePath());
    vector<CvPoint> points;
    points = reader->readPoints();
    
    GenericAdapter* pointsAdapter = new GenericAdapter(LOW_2_HIGH, this->metaInfo);
    vector<CvPoint> pointsAdapted = pointsAdapter->adaptPoints(points);
    
    if(this->isRotated) {
        CoordinatesInverter* inverter = new CoordinatesInverter(pointsAdapted, this->highResolutionImage);
        pointsAdapted = inverter->invertPoints();
    }
    
    vector<CvPoint>::iterator it_points;
    int indexROI = 0;
    for(it_points=pointsAdapted.begin(); it_points != pointsAdapted.end();) {
        CvPoint leftUpper = (*it_points);
        CvPoint rightBottom = (*(++it_points));
        
        ROI* r = new ROI(leftUpper, rightBottom,
               this->metaInfo->getHighResolutionImagePath(),
               this->metaInfo->getLowResolutionImagePath(),
               this->metaInfo->getLowResolutionImageCutIncompletePath(),
               this->metaInfo->getHighResolutionImageCutIncompletePath(),
               this->metaInfo->getModelPreviewIncompletePath(), 
               this->metaInfo->getLogImageIncompletePath(),
               this->isRotated, 
               indexROI++);
        
        this->rois.push_back(r);
              
        it_points++;
        
//        cutAndSaveLowImage(r);
    }
}

/**
 * Returns the vector of rois.
 * 
 * @return vector of rois
 */
vector<ROI*> VectorROIsCreator::getVectorOfROIs() {
    return this->rois;
}


